package com.wodwl.dao;

import java.io.IOException;

import java.io.Reader;

import java.util.List;

import com.ibatis.common.resources.Resources;

import com.ibatis.sqlmap.client.SqlMapClient;

import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.wodwl.entity.App;

public class AppDao {

	public static enum Mode {

		INSERT, UPDATE, DELETE

	}
	public void update(Object arg, Mode mode) {

		SqlMapClient smc = null;

		try {

			smc = getSqlMapClient();
			smc.startTransaction();

			switch (mode) {

			case INSERT:
				smc.insert("appl.insertApp", arg);
				break;

			case UPDATE:
				smc.update("appl.updateAppByAid", arg);
				break;

			case DELETE:
				smc.delete("appl.deleteApp", arg);
				break;

			}

			smc.commitTransaction();

		} catch (Exception e) {

			e.printStackTrace();

		} finally {
			endTransaction(smc);
		}
	}
	public List<App> getAppsByUsername(String username) {
		SqlMapClient smc = null;
		List<App> Apps=null;
		try {
			smc = getSqlMapClient();
			smc.startTransaction();
			Apps =(List<App>) smc.queryForList("appl.getAppsByUsername", username);
			smc.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			endTransaction(smc);
		}
		return Apps;

	}
	public List<App> getAllApps() {
		SqlMapClient smc = null;
		List<App> Apps=null;
		try {
			smc = getSqlMapClient();
			smc.startTransaction();
			Apps =(List<App>) smc.queryForList("appl.getAllApps", null);
			smc.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			endTransaction(smc);
		}
		return Apps;

	}

	public App getAppByAid(int id) {
		App App = null;
		SqlMapClient smc = null;
		try {
			smc = getSqlMapClient();
			smc.startTransaction();
			App = (App) smc.queryForObject("appl.getAppByAid", id);
			smc.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			endTransaction(smc);

		}
		return App;

	}

	// //
	// ////////////////////////////////////////////////////////////////////////

	private SqlMapClient getSqlMapClient() throws IOException {

		// 初始化ibatis, 获得一个SqlMapClient对象

		String resource = "ibatis/SqlMapConfig.xml";
		Reader reader = Resources.getResourceAsReader(resource);
		return SqlMapClientBuilder.buildSqlMapClient(reader);

	}

	private void endTransaction(SqlMapClient smc) {

		if (smc == null) {
			return;
		}
		try {
			smc.endTransaction();
		} catch (Exception e2) {
			e2.printStackTrace();
		}

	}

	public static void main(String[] args) {
		AppDao dao = new AppDao();
		App app=new App();
		
		/*
		 //增
		app.setName("one");
		app.setLeader("two");
		dao.update(app, Mode.INSERT);
		*/
		
		
		//查
		app=dao.getAppByAid(6);
		System.out.println(app.getName());
		
		
		/*
		//改
		app.setAid(1);
		app.setName("update");
		dao.update(app, Mode.UPDATE);
		*/
		
		//删
		//dao.update(1, Mode.DELETE);
		
		List list=dao.getAllApps();
		// List<App> list=dao.getAppsByUsername("aaa");
		System.out.println(list.size());
	}

}